	
	import com.utils.BasicService;
	
	import models.Bookmark;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ArrayUtil;
	import mx.utils.ObjectUtil;
	
	/* private static var instance:SelectCourseTitleWindow = this;
	
	public static function getInstance():SelectCourseTitleWindow
	{
		return instance;
	}
	 */
	
	
	private function openBook():void
	{
		 if (lstBooks.selectedIndex < 0) return;
		Application.application.bookId = lstBooks.selectedItem.id.value;
		Application.application.numberOfPages = lstBooks.selectedItem["number-of-pages"].value;
 		trace(Application.application.numberOfPages);
 		
		//open the 1st page of this book
		Application.application.pageImage.source = Application.application.getImageSource(Application.application.bookId,1); 
		Application.application.pageNumber = 1;
				
		Application.application.cleanPage(); // delete all bookmarks
		
		PopUpManager.removePopUp(this);
		
		loadBookmarksOfABook();
		//Application.application.loadPageRegions();
	}
	
	
	
	private function loadBookmarksOfABook():void
	{
		//TODO correct the url
       	var url:String 	=  Application.application.backEndURL;
       	url +=  "books/" + Application.application.bookId;
//       	url += "/pages/2/";
       	url += "/book_bookmarks.xml"; 
		var service:BasicService = new BasicService(url,"GET",true,bookmarksResult);
		service.send();
	}
	
	
	private function bookmarksResult(event:ResultEvent):void
	{
		var bookmarks:Array = [];
		trace (ObjectUtil.toString(event.result));
		if(event.result.bookmarks != null)
		{
			if (event.result.bookmarks.bookmark is ArrayCollection)
				bookmarks = event.result.bookmarks.bookmark.source;
			else
				bookmarks = ArrayUtil.toArray(event.result.bookmarks.bookmark);
		}
		
		// reset all bookmarks
		for(var j:int=0; j < Application.application.sowarArrCollection.length; j++)
		{
			Application.application.sowarArrCollection[j].bookmark = null;
		}
		
		for (var i:int=0;i<bookmarks.length ; i++)
		{
			trace('id='+bookmarks[i].id.value);
			for(var j:int=0; j < Application.application.sowarArrCollection.length; j++)
			{
				if(Application.application.sowarArrCollection[j].data == bookmarks[i].value)
				{
					Application.application.sowarArrCollection[j].bookmark =  new Bookmark(
						bookmarks[i].id.value, bookmarks[i].x.value, bookmarks[i].y.value,
						bookmarks[i].width.value, bookmarks[i].height.value, bookmarks[i]["page-id"].value);
					break;
				}
			}
		}
		
	}		

	private function cancel():void
	{
		PopUpManager.removePopUp(this);
	}
	
	
	public function retrieveBooks():void
	{
		if (Application.application.isBooksLoaded)	
			return;
		
		//retrieve all books
       	var url:String 	=  Application.application.backEndURL+ "books.xml";
		var service2:BasicService = new BasicService(url,"GET",true,booksResult);
		service2.send();
					
		Application.application.isBooksLoaded = true;
	}
	
	
	
	    
    private function booksResult(event:ResultEvent):void
    {
    	trace(ObjectUtil.toString(event.result));
    	//retrieve all books and save them as an array 
    	var books:Array;
         if(event.result.books.book is ArrayCollection) {
         	books = event.result.books.book.source;
         }else {
         	books = ArrayUtil.toArray(event.result.books.book);
         }
         				
        Application.application.books = books;
    	
    }
    

    
   private function httpFault(event:FaultEvent):void 
    {
         Alert.show(event.fault.faultString);
    }	

	
	
    
    
    